package base.thread_JUC.demo07;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 线程池作用？
 * 线程池的工作是控制运行的线程数量，处理过程中将任务放入队列，然后等线程创建后启动这些任务
 * 1、线程复用；
 * 2、控制最大并发数
 */
public class Client {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(5);//一池5个处理线程
//        ExecutorService executorService = Executors.newSingleThreadExecutor();//一池1个处理线程
//        ExecutorService executorService = Executors.newCachedThreadPool();//一池N个处理线程,放开TimeUnit.MILLISECONDS.sleep(200);试试
        // 模拟10个用户来办理业务，每个用户就是一个来自外部的请求线程
        try {
            for (int i = 1; i <= 10; i++) {
                executorService.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"\t 办理业务");
                });
//                TimeUnit.MILLISECONDS.sleep(200);
            }
        }catch (Exception e){

        }finally {
            executorService.shutdown();
        }
    }

}
